// pages/delivery-time/delivery-time.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: '请选择您要的发货时间',
    addressList:[],
    number: 1,
    maxStock: 99,
    startTime: new Date().getTime()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var pages = getCurrentPages();//当前页面
      var prevPage = pages[pages.length - 2];//上一页面
      let addressList = prevPage.data.addressList;
      if(wx.getStorageSync("addressList")){
        var newList = JSON.parse(wx.getStorageSync("addressList"));
      }else{
        var newList = [{num: 0}];
      }
      this.setData({
          newList: newList
      });
      // let addressList = JSON.parse(wx.getStorageSync("addressList"));
      let index= ~~options.index;
      let orderItem = JSON.parse(wx.getStorageSync("orderItem"));
      this.setData({
          addressList:addressList,
          index:index,
          addressInfo:addressList[index],
          orderItem:orderItem,
          maxStock:orderItem.productNum,
      });
      console.log(123,this.data.maxStock)
      if(options.pickNum!='undefined'){
        this.setData({
          number: ~~options.pickNum,
          pickNum: ~~options.pickNum,
          addIndex: ~~options.addIndex,
          'addressInfo.distributionTime': options.time,
          time: options.time
        })
      }
  },
  changeDate: function (e) {
    let addressInfo = this.data.addressInfo;
    addressInfo.distributionTime =  new Date(e.detail.value).getTime();
    // addressInfo.distributionTime =  e.detail.value;
    let addressList =  this.data.addressList;
    addressList[this.data.index] = addressInfo;
    this.setData({
        addressList:addressList,
        addressInfo:addressList[this.data.index],
    })
  },
  numberSub: function() {
    this.setData({
        number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
    });
  },
  numberAdd: function(e) {
    var num = e.currentTarget.dataset.num-1;
    if(num>=0){
      this.setData({
          number: this.data.number + 1
      });
    }
  },
  goBack: function () {
    if(!this.data.addressInfo.distributionTime){
      wx.showToast({
          title: '请选择发货时间', //提示的内容,
          icon: "none", //图标,
          mask: false, //显示透明蒙层，防止触摸穿透,
      });
      setTimeout(function() {
          wx.hideToast();
      }, 1500);
      return
    }
    if(this.data.pickNum){
      let index = this.data.addIndex
      var addressList = JSON.parse(wx.getStorageSync("addressList"));
      addressList[index].num = this.data.number;
      addressList[index].distributionTime = this.data.addressInfo.distributionTime;
    }else{
      this.setData({
        ['addressInfo.num']: this.data.number
      })
      if(wx.getStorageSync("addressList")){
        var addressList = JSON.parse(wx.getStorageSync("addressList"));
      }else{
        var addressList = [];
      }
      addressList.push(this.data.addressInfo)
    }
    wx.setStorageSync("addressList", JSON.stringify(addressList));
    var pages = getCurrentPages();//当前页面
    var prevPage = pages[pages.length - 3];//上一页面
    prevPage.setData({//直接给上移页面赋值
      addressList: addressList
    });
    wx.navigateBack({
      delta: 2
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})